﻿using LoveNote.Application.Share.Login;
using LoveNote.Database.Entities;
using LoveNote.Database.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Bindings;
using TookKit.Mvvm.Enums;

namespace LoveNote.Application.Note.ViewModels
{
    [DependencyInjection(Lifetime.Singleton)]
    internal class NoteViewModel : ObservableObject
    {      

        private List<NoteEntity> notes;
        public List<NoteEntity> Notes
        {
            get { return notes; }
            set { SetProperty(ref notes, value); }
        }

        private NoteEntity note;
        public NoteEntity Note
        {
            get { return note; }
            set { SetProperty(ref note, value); }
        }

        public ICommand LoadedCommand { get; }
        public ICommand UnloadedCommand { get; }
        public ICommand CreateNoteCommand { get; }  
        public ICommand DeleteNoteCommand { get; }

        private INoteRepository NoteRepository { get; }
        public ISession Session { get; }
        public NoteViewModel(ISession session, INoteRepository noteRepository)
        {
            Session = session;
            NoteRepository = noteRepository;
            LoadedCommand = new RelayCommand(OnLoadedCommand);
            CreateNoteCommand = new RelayCommand(OnCreateNoteCommand);
            UnloadedCommand = new RelayCommand(OnUnloadedCommand);
            DeleteNoteCommand = new RelayCommand<NoteEntity>(OnDeleteNoteCommand);
        }

        /// <summary>
        /// 删除便签 
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OnDeleteNoteCommand(NoteEntity entity)
        {
            var result = Session.Show("是否执行当前操作？", System.Windows.MessageBoxButton.YesNo);
            if (result == null || result.Value == false)
                return;

            int count = NoteRepository.Delete(entity);
            if (count > 0)
            {
                LoadNotes();
            }
        }

        /// <summary>
        /// 保存当前数据
        /// </summary>
        private void OnUnloadedCommand()
        {
            NoteRepository.SaveChanged();
        }

        private void OnCreateNoteCommand()
        {
            CreateNote();
            LoadNotes();
        }

        private void OnLoadedCommand()
        {
            LoadNotes();
        }

        /// <summary>
        /// 加载当前用户的所有便签
        /// </summary>
        private void LoadNotes()
        {
            Notes = NoteRepository.GetAll()
                .FindAll(t => t.UserId == Session.CurrentUser.Id)
                .OrderByDescending(t => t.InsertDate)
                .ToList();
        }

        /// <summary>
        /// 创建当前用户的新便签
        /// </summary>
        private void CreateNote()
        {
            var entity = new NoteEntity();
            entity.InsertDate = DateTime.Now;
            entity.UserId = Session.CurrentUser.Id;
            entity.Content = "请在此输入便签内容...";
            int count = NoteRepository.Insert(entity);
            if(count > 0)
            {
                LoadNotes();
            }
        }
    }
}
